package com.lastproject.ejb.util;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.EmployeeDTO;
import com.lastproject.ejb.dao.RoleDAORemote;
import com.lastproject.ejb.entities.Employee;
import com.lastproject.ejb.entities.Role;

@Stateless
@LocalBean
public class EmployeeConversion {

	@PersistenceContext
	EntityManager em;
	
	List<Role> theRoleList; 
	RoleDAORemote roleDAO;

	public EmployeeDTO fromEntity(Employee employee) {

		EmployeeDTO employeeDTO = new EmployeeDTO();
		employeeDTO.setProsisNr(employee.getProsisNr());
		employeeDTO.setCareerLevel(employee.getCareerLevel());
		employeeDTO.setGermanContactPerson(employee.getGermanContactPerson());
		employeeDTO.setWorkEmail(employee.getWorkEmail());
		employeeDTO.setWorkPhone(employee.getWorkPhone());
		employeeDTO.setId(employee.getId());
		employeeDTO.setUsername(employee.getUsername());
		employeeDTO.setPassword(employee.getPassword());
		employeeDTO.setStatusPassword(employee.getStatusPassword());
		employeeDTO.setFirstname(employee.getFirstname());
		employeeDTO.setLastname(employee.getLastname());
		employeeDTO.setDob(employee.getDob());
		employeeDTO.setPersonalEmail(employee.getPersonalEmail());
		employeeDTO.setPersonalPhone(employee.getPersonalPhone());
		employeeDTO.setEmploymentDate(employee.getEmploymentDate());
		employeeDTO.setDepartureDate(employee.getDepartureDate());
		employeeDTO.setMedCheckUpDate(employee.getMedCheckUpDate());
		employeeDTO.setEmploymentType(employee.getEmploymentType());
		employeeDTO.setPosition(employee.getPosition());
		employeeDTO.setCv(employee.getCv());
		
		// not sure if working
		employeeDTO.setDepartmentManager(employee.isDepartmentManager());
		employeeDTO.setTeamLeader(employee.isTeamLeader());
		
		
		return employeeDTO;

	}

	public Employee fromDTO(EmployeeDTO employeeDTO) {

		Employee employee = em.find(Employee.class, employeeDTO.getId());

		if (employee == null) {
			employee = new Employee();
		}

		employee.setUsername(employeeDTO.getUsername());
		employee.setPassword(employeeDTO.getPassword());
		employee.setStatusPassword(employeeDTO.getStatusPassword());
		employee.setFirstname(employeeDTO.getFirstname());
		employee.setLastname(employeeDTO.getLastname());
		employee.setDob(employeeDTO.getDob());
		employee.setPersonalPhone(employeeDTO.getPersonalPhone());
		employee.setPersonalEmail(employeeDTO.getPersonalEmail());
		employee.setEmploymentDate(employeeDTO.getEmploymentDate());
		employee.setEmploymentType(employeeDTO.getEmploymentType());
		employee.setDepartureDate(employeeDTO.getDepartureDate());
		employee.setMedCheckUpDate(employeeDTO.getMedCheckUpDate());
		employee.setPosition(employeeDTO.getPosition());
		employee.setCv(employeeDTO.getCv());
		employee.setProsisNr(employeeDTO.getProsisNr());
		employee.setWorkEmail(employeeDTO.getWorkEmail());
		employee.setWorkPhone(employeeDTO.getWorkPhone());
		employee.setGermanContactPerson(employeeDTO.getGermanContactPerson());
		employee.setCareerLevel(employeeDTO.getCareerLevel());
		
//		for(Integer i : employeeDTO.getRoleIDList()) {
//			Role r = new RoleConversion().fromDTO(roleDAO.findById(employeeDTO.getRoleIDList().get(i)));
//			theRoleList.add(r);
//		}
//		
//		employee.setRoles(theRoleList);

		employee.setDepartmentManager(employeeDTO.isDepartmentManager());
		employee.setTeamLeader(employeeDTO.isTeamLeader());
		
		return employee;
	}

}
